from covdata import EpidemicSituation
from pyecharts import options as opts
from pyecharts.charts import Geo, Bar, Pie, Line, Liquid
from pyecharts.globals import ChartType

""""
    描述：绘图模块
    功能：绘制和疫情有关的图表
"""


class CovCharts:

    def __init__(self):
        # 从数据模块取数据
        self.cov_info = EpidemicSituation()

    def map_base(self):
        city_map = (
            Geo()
            .add_schema(
                maptype="china",
            )
            .add("", self.cov_info.danger_areas, type_=ChartType.EFFECT_SCATTER)
            .set_series_opts(
                label_opts=opts.LabelOpts(is_show=False),
            )
            .set_global_opts(
                visualmap_opts=opts.VisualMapOpts(is_show=True, pos_left='20%', pos_bottom='0%'),
                title_opts=opts.TitleOpts(title="现存风险地区可视化", pos_left='37%', pos_top='14%'),
                legend_opts=opts.LegendOpts(
                    is_show=False
                )
            )
        )
        return city_map

    def geo_current_confirm(self):
        c = (
            Geo(init_opts=opts.InitOpts())
            .add_schema(
                maptype="china",
                itemstyle_opts=opts.ItemStyleOpts()
            )
            .add(
                "",
                zip(self.cov_info.city_name_list, self.cov_info.current_confirm_nums),
                type_=ChartType.EFFECT_SCATTER,
        )
            .set_series_opts(
                label_opts=opts.LabelOpts(is_show=False),
            )
            .set_global_opts(
                visualmap_opts=opts.VisualMapOpts(is_show=True, pos_left='20%', pos_bottom='0%'),
                title_opts=opts.TitleOpts(title="现存确诊病例可视化", pos_left='37%', pos_top='14%'),
                legend_opts=opts.LegendOpts(
                    is_show=False,
                )
            )
        )
        return c

    def geo_suspected_confirm(self):
        c = (
            Geo(init_opts=opts.InitOpts())
            .add_schema(
                maptype="china",
                itemstyle_opts=opts.ItemStyleOpts()
            )
            .add(
                "",
                zip(self.cov_info.suspected_city_name, self.cov_info.suspected_count_geo),
                type_=ChartType.EFFECT_SCATTER,
            )
            .set_series_opts(
                label_opts=opts.LabelOpts(is_show=False),
            )
            .set_global_opts(
                visualmap_opts=opts.VisualMapOpts(is_show=True, pos_left='20%', pos_bottom='0%'),
                title_opts=opts.TitleOpts(title="现存境外输入可视化", pos_left='37%', pos_top='14%'),
                legend_opts=opts.LegendOpts(
                    is_show=False,
                )
            )
        )
        return c

    def line_base(self):
        currency = self.cov_info.count_suspected_currency()
        c = (
            Line()
            .add_xaxis(self.cov_info.cov_date[1:])
            .add_yaxis(
                is_symbol_show=False,
                linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
                label_opts=opts.LabelOpts(is_show=False),
                series_name="",
                y_axis=currency,
                is_smooth=True,
                markpoint_opts=opts.MarkPointOpts(
                    data=[
                        opts.MarkPointItem(type_="max", name="最大值", symbol_size='30'),
                        opts.MarkPointItem(type_="min", name="最小值", symbol_size='30'),
                    ]
                ),
                markline_opts=opts.MarkLineOpts(
                    data=[
                        opts.MarkLineItem(type_="average", name="平均值"),
                        opts.MarkLineItem(symbol="none", x="90%", y="max"),
                    ],
                    symbol_size=5,
                    linestyle_opts=opts.LineStyleOpts(color='#6F7DE1', opacity=0.8, type_='dashed')
                ),

            )
            .set_global_opts(
                tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
                title_opts=opts.TitleOpts(title="境外输入变化趋势", pos_left='32%', pos_top='6%'),
                datazoom_opts=[opts.DataZoomOpts(type_="inside", xaxis_index=0, is_zoom_lock=False, range_start=10,
                                                 range_end=100), ],
            )
        )
        return c

    def bar_base_of_current_confirm(self) -> Bar:
        if len(self.cov_info.current_confirm_nums_with_city) > 10:
            x = [x[-1] for x in self.cov_info.current_confirm_nums_with_city[0:10]]
            y = [y[0] for y in self.cov_info.current_confirm_nums_with_city[0:10]]
        else:
            x = [x[-1] for x in self.cov_info.current_confirm_nums_with_city]
            y = [y[0] for y in self.cov_info.current_confirm_nums_with_city]
        x = x[::-1]
        y = y[::-1]
        c = (
            Bar()
            .add_xaxis(x)
            .add_yaxis(
                "现存确诊",
                y,
                itemstyle_opts=opts.ItemStyleOpts(color="#7999FF"),
                label_opts=opts.LabelOpts(is_show=False),
            )
            .reversal_axis()
            .set_global_opts(
                tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
                title_opts=opts.TitleOpts(title="现存确诊主要分布状况", pos_left='30%', pos_top='7%'),
                legend_opts=opts.LegendOpts(
                    is_show=False
                ),
            )
            .set_series_opts(
                label_opts=opts.LabelOpts(position="right"),
            )
        )
        return c

    def pie_base(self) -> Pie:
        inner_x_data = self.cov_info.danger_city_name
        inner_y_data = self.cov_info.danger_city_value
        for i in range(0, len(inner_x_data)):
            inner_x_data[i] = inner_x_data[i].replace('省', '').replace('市', '')

        if len(inner_x_data) == 0:
            inner_x_data.append('无风险')
        if len(inner_y_data) == 0:
            inner_y_data.append(0)
        inner_data_pair = [list(z) for z in zip(inner_x_data, inner_y_data)]
        c = (
            Pie(init_opts=opts.InitOpts())
            .add(
                series_name="中高风险地区",
                data_pair=inner_data_pair,
                radius=["32%", "53%"],
                label_opts=opts.LabelOpts(position="inner"),
            )
            .set_global_opts(
                title_opts=opts.TitleOpts(
                    title="现存风险地区",
                    pos_left='38%',
                    pos_top='14%',
                    title_link='https://ncov.dxy.cn/ncovh5/view/pneumonia_risks?from=dxy&link=&share=&source='
                ),
                legend_opts=opts.LegendOpts(is_show=False)
            )
            .set_series_opts(
                tooltip_opts=opts.TooltipOpts(
                    trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
                )
            )
        )
        return c

    def line_current_confirm_base(self):
        confirm = self.cov_info.current_confirm
        c = (
            Line()
            .add_xaxis(self.cov_info.cov_date)
            .add_yaxis(
                "现存确诊",
                confirm,
                is_symbol_show=False,
                areastyle_opts=opts.AreaStyleOpts(opacity=0.3, color='#6F7DE1'),
                is_smooth=True,
                linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
                label_opts=opts.LabelOpts(is_show=False),

            )
            .set_global_opts(
                tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
                title_opts=opts.TitleOpts(title="现存确诊变化趋势", pos_left='33%', pos_top='7%'),
                legend_opts=opts.LegendOpts(
                    is_show=False
                ),
                datazoom_opts=[opts.DataZoomOpts(type_="inside", xaxis_index=0, is_zoom_lock=False, range_start=10,
                                                 range_end=100), ],
            )
        )
        return c

    def line_serious_base(self):
        serious = self.cov_info.serious_count
        c = (
            Line()
            .add_xaxis(self.cov_info.cov_date)
            .add_yaxis(
                "现存无症状",
                serious,
                is_smooth=True,
                is_symbol_show=False,
                linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
                areastyle_opts=opts.AreaStyleOpts(opacity=0.5, color='#00CCFF'),
                label_opts=opts.LabelOpts(is_show=False)

            )
            .set_global_opts(
                tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
                title_opts=opts.TitleOpts(title="现存无症状变化趋势", pos_left='33%', pos_top='7%'),
                legend_opts=opts.LegendOpts(
                    is_show=False
                ),
                datazoom_opts=[opts.DataZoomOpts(type_="inside", xaxis_index=0, is_zoom_lock=False, range_start=10,
                                                 range_end=100), ],
            )
        )
        return c

    def liquid_base_for_cure(self):
        c = (
            Liquid()
            .add(
                "治愈率",
                [self.cov_info.cure_rate],
                label_opts=opts.LabelOpts(
                    font_size=20,
                    position="inside",
                ),
            )
            .set_global_opts(title_opts=opts.TitleOpts(title="治愈率", pos_top='14%', pos_left='35%'))
        )
        return c

    def liquid_base_for_death(self):
        c = (
            Liquid()
            .add(
                "死亡率",
                [self.cov_info.death_rate],
                label_opts=opts.LabelOpts(
                    font_size=20,
                    position="inside",
                )
            )
            .set_global_opts(title_opts=opts.TitleOpts(title="死亡率", pos_top='14%', pos_left='35%'))
        )
        return c
